<%@page import="connection.ConnectionWrapper"%>
<%@page import="model.News"%>


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page language="java" import="java.sql.*,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- includes -->
<%@ include file="includes/includes.jsp"%>
<!-- fin includes -->
<title>JSPBlog search ....</title>
</head>
<body>
<!-- cabecera -->
<%@ include file="includes/header.jsp"%>
<!-- fin cabecera -->

<%!

/*
public int nullIntconv(String str){
    int conv=0;
    if(str==null)
    {
        str="0";
    }
    else if((str.trim()).equals("null"))
    {
        str="0";
    }
    else if(str.equals(""))
    {
        str="0";
    }
    try{
        conv=Integer.parseInt(str);
    }
    catch(Exception e)
    {
    }
    return conv;
}
*/

%>

<div id="main">
<div class="article">
<table height="136" border="0" cellpadding="0"	cellspacing="0" width="100%">
	<tbody>
		<tr>
			<td class="menu" valign="top"><a href="#TOP"></a> <!-- contenido central -->

			<%


    		    int iShowRows=5;  // Numero de registros por pagina
    		    int iTotalSearchRecords=10;  // Numero total de registros

    		    int iTotalRows= Integer.parseInt(request.getParameter("iTotalRows"));
    		    int iTotalPages=Integer.parseInt(request.getParameter("iTotalPages"));
    		    int iPageNo=Integer.parseInt(request.getParameter("iPageNo"));
    		    int cPageNo=Integer.parseInt(request.getParameter("cPageNo"));

    		    int iStartResultNo=0;
    		    int iEndResultNo=0;



				String busqueda = (String) request.getParameter("q");

				String  maxregistros =  application.getInitParameter("maxreg");
				Integer numreg = Integer.parseInt(maxregistros);

				News noticias = new News();
				List<News> lista = noticias.getNews(busqueda ,numreg);

				iTotalRows = lista.size();
				Iterator iter = lista.iterator();
				Integer cont = 0 ;
				while (iter.hasNext()) {
					cont ++;
					System.out.println(iter.next() + "cont : " + cont.toString());
				}

				%>

			<div>
				<%
				        //// index of pages

				        int i=0;
				        int cPage=0;

				        if(iTotalRows!=0)
				        {
				        cPage=((int)(Math.ceil((double)iEndResultNo/(iTotalSearchRecords*iShowRows))));

				        int prePageNo=(cPage*iTotalSearchRecords)-((iTotalSearchRecords-1)+iTotalSearchRecords);
				        if((cPage*iTotalSearchRecords)-(iTotalSearchRecords)>0)
				        {
				         %>
				          <a href="search.jsp?iPageNo=<%=prePageNo%>&cPageNo=<%=prePageNo%>"> << Previous</a>
				         <%
				        }


				        for(i=((cPage*iTotalSearchRecords)-(iTotalSearchRecords-1));i<=(cPage*iTotalSearchRecords);i++)
				        {
				          if(i==((iPageNo/iShowRows)+1))
				          {
				          %>
				           <a href="search.jsp?iPageNo=<%=i%>" style="cursor:pointer;color: red"><b><%=i%></b></a>
				          <%
				          }
				          else if(i<=iTotalPages)
				          {
				          %>
				           <a href="search.jsp?iPageNo=<%=i%>"><%=i%></a>
				          <%
				          }
				        }
				        if(iTotalPages>iTotalSearchRecords && i<iTotalPages)
				        {
				         %>
				         <a href="index.jsp?iPageNo=<%=i%>&cPageNo=<%=i%>"> >> Next</a>
				         <%
				        }
				        }
				      %>

				<b>Rows <%=iStartResultNo%> - <%=iEndResultNo%>   Total Result  <%=iTotalRows%> </b>
				</div>

				<%

				String body = "";
				String tittle = "";
				String author = "";
				String date = "";

				for (News n : lista)
				{

					tittle = n.getTittle();
					date = n.getDate();
					author = n.getAuthor();
					body = n.getBody();
				%>
					<div class="article">
					<form name="f">
					<table border='0' cellpadding="0" align="center" class="blog" width="100%">
						<tr>
							<td><b><%=tittle%></b></td>
						</tr>
						<tr>
							<td><b><%=date%></b></td>
						</tr>
						<tr>
							<td><i><%=author%></i></td>
						</tr>
						<tr>
							<td><%=body%></td>
						</tr>
					</table>
					<input type="hidden" name="iPageNo" value="<%=iPageNo%>">
					<input type="hidden" name="cPageNo" value="<%=cPageNo%>">
					<input type="hidden" name="iShowRows" value="<%=iShowRows%>">
					</form>

					</div>
					<br />
					<hr>
					</hr>
			<%

				} // Final del for

			%>

			 <!-- fin contenido central --></td>
		</tr>
	</tbody>
</table>

</div>
</div>

<!-- menu  -->
<%@ include file="includes/menu.jsp"%>
<!--  fin menu  -->

<!-- footer  -->

<%@ include file="includes/footer.jsp"%>
<!--  fin footer -->

</body>
</html>